package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonMessageStatusMapper;
import com.yungu.swift.common.model.dto.CommonMessageStatusDto;
import com.yungu.swift.common.model.dto.CommonSystemMessageDto;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Copyright © 厦门云谷互联信息科技有限公司 2018-2020 All Right Reserved
 * <p>
 * 系统消息业务逻辑接口实现类
 *
 * @author Zerrion
 * @version 1.1.0-SNAPSHOT
 */
@Service
public class CommonMessageStatusServiceImpl extends BaseServiceImpl<CommonMessageStatusDto> implements CommonMessageStatusService {

    @Autowired
    private CommonMessageStatusMapper commonMessageStatusMapper;

    @Override
    protected IMapper<CommonMessageStatusDto> getMapper() {
        return commonMessageStatusMapper;
    }

    @Override
    public ResponseData delMessagesByUuids(String userUuid, String uuids) {
        //暂时不做对删除消息的正确性做判断
        //循环逻辑删除
        String[] uuidArray = org.springframework.util.StringUtils.commaDelimitedListToStringArray(uuids);
        for (String uuid : uuidArray) {
            CommonMessageStatusDto commonMessageStatusDto = new CommonMessageStatusDto();
            commonMessageStatusDto.setUuid(uuid);
            commonMessageStatusDto.setStatus(CommonConstant.MSG_STATUS_DELETED);
            commonMessageStatusDto.setUpdateBy(userUuid);
            commonMessageStatusMapper.edit(commonMessageStatusDto);
        }
        return ResponseData.buildSuccessResponse("删除成功", Boolean.TRUE);
    }

    @Override
    public ResponseData<Boolean> delHomeMessagesByUuid(String uuid) {
        CommonMessageStatusDto commonMessageStatusDto = new CommonMessageStatusDto();
        commonMessageStatusDto.setUuid(uuid);
        CommonMessageStatusDto dto = commonMessageStatusMapper.list(MapUtils.build("uuid", uuid)).get(0);
        if (dto.getStatus() == CommonConstant.MSG_STATUS_NOREAD) {
            commonMessageStatusDto.setStatus(CommonConstant.MSG_STATUS_HOME_UNREAD_DELETED);
        } else {
            commonMessageStatusDto.setStatus(CommonConstant.MSG_STATUS_HOME_READ_DELETED);
        }
        return ResponseData.buildSuccessResponse("删除成功", commonMessageStatusMapper.edit(commonMessageStatusDto) > 0);
    }

    @Override
    public ResponseData<Boolean> delAllMsg(String userUuid) {
        CommonMessageStatusDto commonMessageStatusDto = new CommonMessageStatusDto();
        commonMessageStatusDto.setStatus(CommonConstant.MSG_STATUS_DELETED);
        commonMessageStatusDto.setTargetUuid(userUuid);
        return ResponseData.buildSuccessResponse("删除成功", commonMessageStatusMapper.edit(commonMessageStatusDto) > 0);
    }


    @Override
    public ResponseData<Integer> addBatch(List<CommonMessageStatusDto> list) {
        int affectedRowCount = commonMessageStatusMapper.addBatch(list);
        return ResponseData.buildSuccessResponse(String.format("插入成功：%s 行", affectedRowCount), affectedRowCount);
    }

    @Override
    public ResponseData<Boolean> addSysMessageStatus(CommonSystemMessageDto systemMessageDto, List<String> userUuids) {
        List<CommonMessageStatusDto> dtos = new ArrayList<>();
        for (String uuid : userUuids) {
            CommonMessageStatusDto messageStatusDto = new CommonMessageStatusDto();
            messageStatusDto.setUuid(StringUtils.buildUUID());
            messageStatusDto.setStatus(CommonConstant.MSG_STATUS_NOREAD);
            messageStatusDto.setType(systemMessageDto.getType());
            messageStatusDto.setMessageUuid(systemMessageDto.getUuid());
            messageStatusDto.setAppid(systemMessageDto.getAppid());
            messageStatusDto.setTargetUuid(uuid);
            messageStatusDto.setTargetType(CommonConstant.ADMIN);
            dtos.add(messageStatusDto);
        }
        return ResponseData.buildSuccessResponse("添加信息状态成功", commonMessageStatusMapper.addBatch(dtos) > 0);
    }

    @Override
    public ResponseData<Boolean> updSysMessageStatus(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(commonMessageStatusMapper.updSysMessageStatus(params) > 0);
    }

    @Override
    public ResponseData<List<String>> getMsgUuids(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(commonMessageStatusMapper.getMsgUuids(params));
    }
}